<template>
  <!-- 系统管理-角色管理-编辑角色 -->
  <div class="form-div">
    <el-form :model="form" label-width="80px" :rules="rules" ref="formRef">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="form.roleName"></el-input>
      </el-form-item>
      <el-form-item label="角色标识" prop="roleType">
        <el-input v-model="form.roleType"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="roleDesc">
        <el-input v-model="form.roleDesc"></el-input>
      </el-form-item>
      <el-form-item label="权限列表" prop="selectedPermissions">
        <div class="permissionList-div">
          <el-checkbox-group v-model="selectedPermissions" :checkbox="echoPermissions" size="large">
            <el-checkbox-button style="margin: 1px" v-for="permission in permissionList" :value="permission.id">
              {{ permission.permissionName }}
            </el-checkbox-button>
          </el-checkbox-group>
        </div>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save(formRef)">保存</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>

import {onMounted, ref} from "vue";
import {getPermissionListApi} from "@/assets/api/system/permission";
import {editRoleApi} from "@/assets/api/system/role";
import {ElMessage} from "element-plus";

const props = defineProps({
  form: {
    roleName: String,
    roleType: String,
    roleDesc: String,
    status: Number,
    remark: String,
  },
  echoPermissions: Array // 选中的权限
});

const emit = defineEmits(['save', 'close']);

// 获取所有权限
const permissionList = ref([]);
const selectedPermissions = ref(props.echoPermissions);
const getPermissionList = () => {
  getPermissionListApi().then(res => {
    permissionList.value = res.data.data;
  })
}

// 表单验证
const formRef = ref();
const permissionValidator = (rule, value, callback) => {
  if (selectedPermissions.value.length === 0) {
    callback(new Error('请选择权限'));
  } else {
    callback();
  }
}
const rules = ref({
  roleName: [
    {required: true, message: '请输入角色名称', trigger: 'blur'},
  ],
  roleType: [
    {required: true, message: '请输入角色标识', trigger: 'blur'},
  ],
  roleDesc: [{required: true, message: '请输入角色标识', trigger: 'blur'}],
  selectedPermissions: [{required: true, validator: permissionValidator, trigger: 'blur'}
],
status: [{required: true, message: '请选择状态', trigger: 'blur'}],
})

// 保存
const save = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      props.form.permissionIds = selectedPermissions.value.join(",");
      editRoleApi(props.form).then(res => {
        if (res.data.code === 200) {
          ElMessage.info("保存成功")
          emit('save', true);
        } else {
          ElMessage.error(res.data.msg)
        }
      })
    }
  })
}

onMounted(() => {
  getPermissionList();
})
</script>

<style scoped>
.permissionList-div {
  border: 1px solid #dcdfe6;
  width: 100%;
  height: 200px;
  overflow: auto;
}
</style>